package Part7_Test;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

/**
 * ClassName: FruitTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.05.09 16:53
 */
public class FruitTest {
    @Test
    public void test1() throws IOException, ClassNotFoundException, NoSuchMethodException,
            InvocationTargetException, InstantiationException, IllegalAccessException {
        //1、读配置文件,获取类名
        Properties pros = new Properties();

        File file = new File("info.properties");
        FileInputStream fis = new FileInputStream(file);

        pros.load(fis);

        String fruitName = pros.getProperty("fruitName");

        //2、反射创建类的对象
        Class<?> aClass = Class.forName(fruitName);
        Constructor<?> constructor = aClass.getDeclaredConstructor();

        constructor.setAccessible(true);
        Fruit fruit = (Fruit) constructor.newInstance();


        //3、调取run()启动squeeze()
        Juicer juicer = new Juicer();
        juicer.run(fruit);
    }
}
